define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'staff/staff/index' + location.search,
                    add_url: 'staff/staff/add',
                    edit_url: 'staff/staff/edit',
                    del_url: 'staff/staff/del',
                    multi_url: 'staff/staff/multi',
                    import_url: 'staff/staff/import',
                    table: 'staff',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'province_text', title: __('省')},
                        {field: 'city_text', title: __('市')},
                        {field: 'image', title: __('封面图'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'service_text', title: __('Service_ids')},
                        {field: 'service1_text', title: __('Service1_ids')},
                        {field: 'height', title: __('Height')},
                        {field: 'weight', title: __('Weight')},
                        {field: 'age', title: __('Age')},
                        {field: 'figure', title: __('Figure')},
                        // {field: 'city', title: __('City'), operate: 'LIKE'},
                        {field: 'character', title: __('Character'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'price', title: __('解锁价格'), operate: 'LIKE'},
                        {field: 'is_share', title: __('是否发帖分享'), searchList: {"0":__('Is_share 0'),"1":__('Is_share 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_vip', title: __('是否VIP'), operate: false, formatter: Controller.api.formatter.custom},
                        {field: 'check_state', title: __('审核状态'), searchList: {"0":__('Check_state 0'),"1":__('Check_state 1'),"2":__('Check_state 2')}, formatter: Table.api.formatter.normal},
                        {field: 'state', title: __('认证状态'), searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'click',
                                    title: __('审核操作'),
                                    text:"审核操作",
                                    classname: 'btn btn-xs btn-info btn-click',
                                    // icon: 'fa fa-leaf',
                                    // dropdown: '更多操作',//如果包含dropdown，将会以下拉列表的形式展示
                                    click: function (data,row) {
                                        Layer.confirm(
                                            '确认审核同意或者拒绝操作?',
                                            {icon: 3,btn:['同意', '拒绝'],  title: __('Warning'), offset: '40%', shadeClose: true},
                                            function (index) {
                                                Fast.api.ajax({
                                                    url: "staff/staff/setcheck/ids=" + row.ids,
                                                    data: {ids:row.ids,check_state:1}
                                                }, function(data, ret){//成功的回调
                                                    if (ret.code === 1) {
                                                        table.bootstrapTable('refresh');
                                                        Layer.close(index);
                                                    } else {
                                                        Layer.close(index);
                                                        Toastr.error(ret.msg);
                                                    }
                                                }, function(data, ret){//失败的回调
                                                    console.log(ret);
                                                    // Toastr.error(ret.msg);
                                                    Layer.close(index);
                                                });
                                            },function (e) {
                                                Fast.api.ajax({
                                                    url: "staff/staff/setcheck/ids=" + row.ids,
                                                    data: {ids:row.ids,check_state:2},
                                                }, function(data, ret){//成功的回调
                                                    if (ret.code === 1) {
                                                        table.bootstrapTable('refresh');
                                                        Layer.close(index);
                                                    } else {
                                                        Layer.close(index);
                                                        Toastr.error(ret.msg);
                                                    }
                                                }, function(data, ret){//失败的回调
                                                    console.log(ret);
                                                    // Toastr.error(ret.msg);
                                                    Layer.close(index);
                                                });
                                            }
                                        );
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.check_state == 0;
                                    }
                                },
                                {
                                    name: 'click',
                                    title: __('认证操作'),
                                    text:"认证操作",
                                    classname: 'btn btn-xs btn-info btn-click',
                                    // icon: 'fa fa-leaf',
                                    // dropdown: '更多操作',//如果包含dropdown，将会以下拉列表的形式展示
                                    click: function (data,row) {
                                        Layer.confirm(
                                            '确认认证同意或者拒绝操作?',
                                            {icon: 3,btn:['同意', '拒绝'],  title: __('Warning'), offset: '40%', shadeClose: true},
                                            function (index) {
                                                Fast.api.ajax({
                                                    url: "staff/staff/setstate/ids=" + row.ids,
                                                    data: {ids:row.ids,state:1}
                                                }, function(data, ret){//成功的回调
                                                    if (ret.code === 1) {
                                                        table.bootstrapTable('refresh');
                                                        Layer.close(index);
                                                    } else {
                                                        Layer.close(index);
                                                        Toastr.error(ret.msg);
                                                    }
                                                }, function(data, ret){//失败的回调
                                                    console.log(ret);
                                                    // Toastr.error(ret.msg);
                                                    Layer.close(index);
                                                });
                                            },function (e) {
                                                Fast.api.ajax({
                                                    url: "staff/staff/setstate/ids=" + row.ids,
                                                    data: {ids:row.ids,check_state:2},
                                                }, function(data, ret){//成功的回调
                                                    if (ret.code === 1) {
                                                        table.bootstrapTable('refresh');
                                                        Layer.close(index);
                                                    } else {
                                                        Layer.close(index);
                                                        Toastr.error(ret.msg);
                                                    }
                                                }, function(data, ret){//失败的回调
                                                    console.log(ret);
                                                    // Toastr.error(ret.msg);
                                                    Layer.close(index);
                                                });
                                            }
                                        );
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.state == 0;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            //自定义Tab筛选条件
            $('.panel-heading .nav-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};

                    delete filter.check_state;
                    delete op.check_state;
                    delete filter.is_vip;
                    delete op.is_vip;

                    //这里可以自定义多个筛选条件
                    switch (value) {
                        case 1:
                            filter.check_state = 0;
                            op.check_state = '=';
                            break;
                        case 2:
                            filter.check_state = 1;
                            op.check_state = '=';
                            break;
                        case 3:
                            filter.check_state = 1;
                            op.check_state = '=';
                            filter.is_vip = 1;
                            op.is_vip = '=';
                            break;
                        default:
                            //选全部时要移除相应的条件
                            delete filter.check_state;
                            delete op.check_state;
                            delete filter.is_vip;
                            delete op.is_vip;
                    }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});
                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', {pageNumber: 1});
                return false;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'staff/staff/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'staff/staff/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'staff/staff/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="staff/staff/setVip" data-confirm="确认切换VIP技师状态？" data-id="' + row.id + '"><i class="fa ' + (row.is_vip == 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            }
        }
    };
    return Controller;
});
